package login;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ibatis.sqlmap.client.SqlMapClient;
import ibatis.IbatisHelper;

public class LoginManager {
	private SqlMapClient ibatis;

	public LoginManager() {
		this.ibatis = IbatisHelper.getSqlMapInstance();
	}
	
	//old user
	public List selectOldUser(String userId, String active, String loggedin) {
		List result = new ArrayList();
		
		//System.out.println("-- : " + userId);
		try {
			Map map = new HashMap();
			map.put("param_userid", userId);
			map.put("param_active", active);
			//map.put("param_loggedin", loggedin);
			
			result = ibatis.queryForList("eClass.getOldUser", map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean UpdateOldUser(UserModel user){		
		boolean result = false;
		
		try{
			ibatis.startTransaction();
			int resultNo = ibatis.update("eClass.updateOldUser", user);
			ibatis.commitTransaction();
			
			if(resultNo == 1)
			{result = true;}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try{
				ibatis.endTransaction();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	//new user
	public List selectNewUser(String userId, String password, String active, String loggedin) {
		List result = new ArrayList();
		
		//System.out.println("-- : " + userId + "  --  " + password);
		
		try {
			Map map = new HashMap();
			map.put("param_userid", userId);
			map.put("param_password", password);
			map.put("param_active", active);
			//map.put("param_loggedin", loggedin);
			
			result = ibatis.queryForList("eClass.getNewUser", map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	public boolean UpdateNewUser(UserModel user){
		boolean result = false;
		
		try{
			ibatis.startTransaction();
			int resultNo = ibatis.update("eClass.updateNewUser", user);
			ibatis.commitTransaction();
			
			if(resultNo == 1)
			{result = true;}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try{
				ibatis.endTransaction();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
		return result;
	}
}